<?php defined('BASEPATH') or exit('No direct script access allowed');
class Agent_m extends MY_Model
{
	public function __construct(){
		parent::__construct();
		$this->set_table_name("transaction");
	}

	// Return yesterday win lose, current month win lose, last month win lose
	public function getBalance($agentId){
		$sql = "SELECT SUM("
		." \nCASE WHEN ("
		." \nt.status = 'WIN'"
		." \nOR t.status = 'LOSE'"
		." \n)"
		." \nAND u.user_type = 'USER'"
		." \nTHEN t.agent_win_lose + t.commission"
		." \nWHEN t.status = 'COMPLETE'"
		." \nAND u.user_type = 'AGENT'"
		." \nTHEN t.win_lose - t.stake"
		." \nELSE 0"
		." \nEND ) AS balance"
		." \nFROM `l_transaction` t"
		." \nINNER JOIN l_user u ON t.user_id = u.user_id "
		." \n WHERE (t.agent_id = ".((int)$agentId)." OR t.user_id = " .((int)$agentId).")"
		." \n AND date(t.settle_date) BETWEEN ? AND ?";

		$yesterday = date("Y-m-d", strtotime("yesterday"));

		$today = new DateTime();
		$today=$today->format('Y-m-d');

		$beginOfCurrentMonth = new DateTime();
		$beginOfCurrentMonth->modify('first day of this month');
		$beginOfCurrentMonth=$beginOfCurrentMonth->format('Y-m-d');

		$lastMonth =date("Y-m-d", strtotime("-1 month"));
		$beginOfLastMonth=new DateTime($lastMonth);
		$beginOfLastMonth->modify('first day of this month');
		$beginOfLastMonth=$beginOfLastMonth->format('Y-m-d');

		$lastOfLastMonth=new DateTime($lastMonth);
		$lastOfLastMonth->modify('last day of this month');
		$lastOfLastMonth=$lastOfLastMonth->format('Y-m-d');

		$yesterdayWinLose = $this->db->query($sql, array($yesterday,$yesterday))->result();
		$currentMonthWinLose=$this->db->query($sql, array($beginOfCurrentMonth,$today))->result();
		$lastMonthWinLose=$this->db->query($sql, array($beginOfLastMonth,$lastOfLastMonth))->result();

		$yesterdayWinLose = $yesterdayWinLose[0]->balance;
		$currentMonthWinLose = $currentMonthWinLose[0]->balance;
		$lastMonthWinLose = $lastMonthWinLose[0]->balance;


		$result = array("yesterday"=>$yesterdayWinLose == NULL? 0:$yesterdayWinLose,
			"currentmonth" =>$currentMonthWinLose == NULL? 0:$currentMonthWinLose,
			"lastmonth" =>$lastMonthWinLose == NULL? 0:$lastMonthWinLose
		);

		return $result;
	}

	public function getPaymentHistory($fromdate,$todate,$agentId){

		$fromdate->modify('first day of this month');
		$fromdate=$fromdate->format('Y-m-d');

		$todate->modify('first day of this month');
		$todate=$todate->format('Y-m-d');

		$sql ="SELECT * FROM l_earning WHERE user_id =? AND date(month) BETWEEN ? AND ? ORDER BY month";
		return $this->db->query($sql, array($agentId, $fromdate,$todate))->result();
	}

	public function getBetList($fromdate, $todate, $agentId){

		$sql="SELECT du.username, t.*"
		." \nFROM `l_transaction` t INNER JOIN l_user u ON t.user_id= u.user_id INNER JOIN default_users du ON du.id=u.user_id"
		." \nWHERE u.user_type ='USER' "
		." \nAND (t.status = 'WIN' or t.status= 'LOSE' or t.status = 'RUNNING')"
		."AND date(t.settle_date) BETWEEN ? AND ? "
		."AND t.agent_id = ?"
		."\n ORDER BY t.trans_date";

		return $this->db->query($sql, array($fromdate->format('Y-m-d'),
		$todate->format('Y-m-d'),
		$agentId
		))->result();
	}

}